﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLLFactory;
using DataMode;
using IBLL;
using System.Data;
using System.Text;

namespace Face.Web.VideoPage
{
    public partial class VideoUpdate : System.Web.UI.Page
    {
        private static IBLL.IVideo bll = BLLFactory.DataAccess.GetVideo();
        private static IBLL.IAlbum bllAlbum = BLLFactory.DataAccess.GetAlbum();
        DataModeDataContext ctx = new DataModeDataContext();
        public string ThisUserName;
        video newvideo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
                Response.Redirect("/login_and_signup/login.aspx");

            ThisUserName = Session["user"].ToString();

            UploadStatusLabel.Text = "";
            newvideo = new DataMode.video();
            

            var q = from myAlbum in ctx.video_album select myAlbum;
            foreach (var M in q.Where(myAlbum => myAlbum.album_user == ThisUserName))
            {
                albumList.Items.Add(M.album_name);
            }
        }

        protected void Button_Sumbition(object sender, EventArgs e)
        {
            try
            {
                if (this.video.HasFile)
                {
                    if (video.FileContent.Length > 1024 * 1024 * 20)//判断文件大小
                    {
                        UploadStatusLabel.Text = "上传视频过大，请选择20M以下视频上传。";
                    }
                    else
                    {
                        string type = video.FileName.Substring(video.FileName.LastIndexOf('.') + 1);
                        if (type == "flv" || type == "wmv")//判断文件类型
                        {
                            string savePath = SaveFile(video.PostedFile);//保存文件
                        }
                        else
                        {
                            UploadStatusLabel.Text = "请选择flv或wmv格式文件。";
                        }
                    }
                }
                else
                {
                    UploadStatusLabel.Text = "请选择上传视频。";
                }


            }
            catch (Exception ex)
            {
                this.UploadStatusLabel.Text = ex.Message;

            }

            try
            {
                if (this.picture.HasFile)
                {
                    if (picture.FileContent.Length > 1024 * 1024 * 5)//判断文件大小
                    {
                        UploadStatusLabel2.Text = "上传图片过大，请选择5M以下图片上传。";
                    }
                    else
                    {
                        string type = picture.FileName.Substring(picture.FileName.LastIndexOf('.') + 1);
                        if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")//判断文件类型
                        {
                            string savePath2 = SavePicture(picture.PostedFile);//保存文件
                            bll.InsertVideo(newvideo);
                        }
                        else
                        {
                            UploadStatusLabel2.Text = "请选择图片格式文件。";
                        }
                    }
                }
                else
                {
                    UploadStatusLabel2.Text = "请选择上传图片。";
                }


            }
            catch (Exception ex)
            {
                this.UploadStatusLabel.Text = ex.Message;

            }
            
            videoName.Text = " ";
            videoDescription.Text = " ";

            //Response.Write("<script language='javascript'>window.location='../VideoPage/MyVideoAlbumsManage.aspx'</script>");
        }

        private string SaveFile(HttpPostedFile file)
        {
            string savePath = Request.PhysicalApplicationPath + @"/videos/";
            string fileName = video.FileName;
            string pathToCheck = savePath + fileName;
            string tempfileName = "";

            if (System.IO.File.Exists(pathToCheck))//判断是否有同名文件存在
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    tempfileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;
                UploadStatusLabel.Text = "有同名视频存在，" +
                    "上传视频被另存为 " + fileName;
            }
            else
            {
                UploadStatusLabel.Text = "视频上传成功。";
            }
            savePath += fileName;
            video.SaveAs(savePath);//保存文件



            newvideo.video_name = videoName.Text;
            newvideo.video_path = @"/videos/"+fileName;
            newvideo.video_description = videoDescription.Text;
            newvideo.username = Session["user"].ToString();
            newvideo.load_time = DateTime.Now;
            newvideo.video_album_name = albumList.SelectedItem.Text;
            newvideo.video_album_id = bllAlbum.GetAlbumIDByAlbumAndUsername(newvideo.video_album_name, newvideo.username);
            newvideo.like_num = 0;
            newvideo.dislike_num = 0;
            newvideo.play_num = 0;
           
            return fileName;

        }

        private string SavePicture(HttpPostedFile file)
        {
            string savePath = Request.PhysicalApplicationPath + @"/videoPicture/";
            string fileName = picture.FileName;
            string pathToCheck = savePath + fileName;
            string tempfileName = "";

            if (System.IO.File.Exists(pathToCheck))//判断是否有同名文件存在
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    tempfileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;
                UploadStatusLabel.Text = "有同名图片存在，" +
                    "上传图片被另存为 " + fileName;
            }
            else
            {
                UploadStatusLabel.Text = "图片上传成功。";
            }
            savePath += fileName;
            picture.SaveAs(savePath);//保存文件
            newvideo.video_img = @"/videoPicture/" + fileName;
            return fileName;

        }

        protected void btn_logout(object sender, EventArgs e)
        {
            Session.Remove("user");
            Response.Redirect("/login_and_signup/login.aspx");
        }
    }
}